package com.winz.cultureapp.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.CommunityDesBean;
import com.winz.cultureapp.entity.UserInfoBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.UserCollectionReq;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文化社团 -- 社团详情
 */
public class CommunityDesActivity extends BaseAppCompatActivity {

    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.iv_right)
    ImageView mIv_right;
    @BindView(R.id.progress)
    ProgressBar mProgress;
    @BindView(R.id.iv_love)
    ImageView mIv_love;
    @BindView(R.id.wv_content)
    WebView mWv_content;
    @BindView(R.id.tv_join)
    TextView mTv_join;



    private String href;
    private String leagueId;
    private CommunityDesBean.DataBean mData;
    private String from;


    @Override
    protected int contentView() {
        return R.layout.activity_community_des;
    }

    @Override
    protected void initViews() {
        mIv_right.setVisibility(View.VISIBLE);
        mIv_right.setImageResource(R.drawable.icon_share);
        mTv_top_title.setText("社团详情");

        mWv_content.getSettings().setJavaScriptEnabled(true);//设置WebView支持JS
        mWv_content.getSettings().setDefaultTextEncodingName("utf-8");//设置解码时的字符集
        mWv_content.getSettings().setLoadWithOverviewMode(true);
        mWv_content.getSettings().setUseWideViewPort(true);
        mWv_content.getSettings().setSupportZoom(true);
        mWv_content.getSettings().setBuiltInZoomControls(true);
        mWv_content.getSettings().setDomStorageEnabled(true);
        mWv_content.getSettings().setBuiltInZoomControls(false);
        mWv_content.setWebViewClient(new WebViewClient() {
            /*
            此处能拦截超链接的url,即拦截href请求的内容.
            */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }

        });
        mWv_content.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    mProgress.setVisibility(View.GONE);//加载完网页进度条消失
                } else{
                    mProgress.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mProgress.setProgress(newProgress);//设置进度值

                }
            }
        });
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null) {
            if (intent.getExtras() != null) {
                href = intent.getExtras().getString(Constants.KEY.COMMUNITY.ITEM_DES_HREF);
                leagueId = intent.getExtras().getString(Constants.KEY.COMMUNITY.ITEM_ID);
                from = intent.getExtras().getString(Constants.KEY.COMMUNITY.FROM);
                if ("my".equals(from)){
                    mTv_join.setText("已加入");
                    mTv_join.setClickable(false);
                    mTv_join.setBackgroundColor(getResources().getColor(R.color.unchecked_color));
                }
                if (TextUtils.isEmpty(leagueId)){
                    showMsg("社团不存在");
                }else{
                    getDes();
                }
                mWv_content.loadUrl(href);
            }
        }
    }

    private void getDes() {
        HttpUitls.onGetByToken("league_detail", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), leagueId, new OnResponeListener<CommunityDesBean>() {
            @Override
            public void onSuccess(CommunityDesBean bean) {
                Logger.d("league_detail==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    mData = bean.data;
                    changeCollection();
                } else {
                    showMsg(bean.msg);
                }
            }

            @Override
            public void onFailed(String error) {
                Logger.d("league_detail==>>", error);

            }
        });
    }

    @OnClick({R.id.fl_back, R.id.rl_right, R.id.ll_collection, R.id.tv_join})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_back:
                finish();
                break;
            case R.id.rl_right:

                break;
            case R.id.ll_collection: // 收藏
                if (mData == null){
                    return;
                }

                collection();
                break;
            case R.id.tv_join: // 加入
                Bundle bundle = new Bundle();
                bundle.putString(Constants.KEY.COMMUNITY.ITEM_ID,leagueId);
                startActivity(JoinCommunityActivity.class, false, bundle);
                break;
        }
    }

    /*
       收藏状态显示
    */
    private void changeCollection() {
        if (mData.collectionStatus == 1) {
            mIv_love.setImageResource(R.drawable.icon_love_sel);
        } else {
            mIv_love.setImageResource(R.drawable.icon_love_nor);
        }
    }

    /**
     * 添加收藏
     */
    private void collection() {
        showProgressDialog();

        HttpUitls.onPostByToken(mData.collectionStatus == 0 ? "user_collection_save" : "user_collection_cancel", SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN), new UserCollectionReq(mData.leagueId, "2"), new OnResponeListener<UserInfoBean>() {
            @Override
            public void onSuccess(UserInfoBean bean) {
                Logger.d(mData.collectionStatus == 0 ? "user_collection_save" : "user_collection_cancel" + "==>>", JsonUtil.getJsonFromObj(bean));
                HttpUitls.isExpire(bean.code);
                if (bean.code == 0) {
                    showMsg(mData.collectionStatus == 0 ? "收藏成功" : "取消收藏");
                    mData.collectionStatus = mData.collectionStatus == 0 ? 1 : 0;
                    changeCollection();
                } else {
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("user_collection_save==>>", error);
            }
        });
    }

}
